package com.example.xwcommon;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class GlobalFallback implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        if(e instanceof DegradeException){
            httpServletResponse.getWriter().write("{\"code\":6000,\"data\":null,\"msg\":服务器繁忙，请稍后重试\"}");
            return;
        }
        if(e instanceof FlowException){
            httpServletResponse.getWriter().write("{\"code\":6000,\"data\":null,\"msg\":服务器限流，请稍后重试\"}");
            return ;
        }
    }
}
